
import json
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time
arrm = {1: [-1, 1],2: [0, 1],3: [1, 1],4: [-1, 0],6: [1, 0],7: [-1, -1],8: [0, -1],9: [1, -1],}
def get_arr(x, y):
    s = '↙↓↘←→↖↑↗'
    co = 0
    def same(a, b):
        if a == 0 and b == 0:
            return True
        if a > 0 and b > 0:
            return True
        if a < 0 and b < 0:
            return True
        return False
    
    for k, v in arrm.items():
        co += 1
        dx, dy = v
        if same(x, dx) and same(y, dy):
            return s[co-1]
    return '□'
    

ss = '''
[["<96>", 96], ["<97>", 97], ["<98>", 98], ["<99>", 99], ["<100>", 100], ["<101>", 101], ["<102>", 102], ["<103>", 103], ["<104>", 104], ["<105>", 105],
["<110>", 110], ["'+'", 107], ["'-'", 109], ["'*'", 106], ["'/'", 111]]
'''
numL = json.loads(ss)

key_enter_time = 0
num_co = 0
def decode(key, pressed):
    global key_enter_time, num_co
    st = str(key)
    if 'Key.enter' in st:
        key_enter_time = time.time()
        return -2
    
    if '<255>' in st and time.time() - key_enter_time < 0.1:
        key_enter_time = 0
        return 15

    if st == 'Key.num_lock':
        if pressed:
            num_co += 1
        if num_co % 2 == 0:            
            return 16
        return -2
        
    if not hasattr(key, 'vk') :
        return -1
    
    for i in range(len(numL)):
        if numL[i] == [str(key), key.vk]:
            return i
    return -1

def deal_sum(sum, a, max_l):
    if len(sum) >= max_l:
        return sum
    if a == 0:
        if sum == '0':
            return sum
        sum += '0'
        return sum
    if a == 10:
        if '.' in sum:
            return sum
        sum += '.'
        return sum
    if sum == '0':
        sum = str(a)
        return sum
    sum += str(a)
    return sum
    
def draw_txt(painter, txt,x,y,yAdd, txtSize, pathSize, isone=False):
    y = y+yAdd
    if txt=='':
        return
  
    path = QPainterPath()
    font = QFont()
    font.setBold(True);        
    font.setFamily("Arial");
    font.setPixelSize(txtSize);            
    path.addText(x, y, font, txt);    
    pen = QPen()
    if isone:
        pen.setColor(Qt.white)
        #pen.setColor(QColor(255, 240, 123));
        #pen.setColor(Qt.black)
    else:        
        pen.setColor(Qt.white);        
    pen.setStyle(Qt.SolidLine);
    pen.setWidth(pathSize);       
    painter.setRenderHint(QPainter.Antialiasing);
    painter.strokePath(path, pen);
    if isone:
        painter.fillPath(path, QBrush(QColor(255, 240, 123)) );
        painter.fillPath(path, QBrush( Qt.white) );
        painter.fillPath(path, QBrush( Qt.black) );
    else:        
        painter.fillPath(path, QBrush(Qt.black));  
        
 
        
def drawCir(p,x,y, isR, w1, r1, w2, r2):
    pen = QPen()
    pen.setWidth(w1)
    pen.setColor(Qt.white)        
    p.setPen(pen)
    p.drawEllipse(QPoint(x,y),r1,r1)
    
    pen.setWidth(w2)
    if isR:
        pen.setColor(Qt.red)
    else:
        pen.setColor(Qt.black)
    p.setPen(pen)
    p.drawEllipse(QPoint(x,y),r2, r2)  